package cn.group7.padserver.service.impl;

import cn.group7.padserver.dao.ShoppingCartDao;
import cn.group7.padserver.entity.ShoppingModel;
import cn.group7.padserver.pojo.ShoppingCart;
import cn.group7.padserver.service.ShoppingCartService;
import jakarta.annotation.Resource;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 谷汉斌
 * @description
 * @createTime 2024/8/4 下午3:23
 */
@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Resource
    private ShoppingCartDao shoppingCartDao;

    @Override
    public List<ShoppingModel> selectByUserOrderId(Integer orderId) {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        ShoppingCart shoppingcart = new ShoppingCart();
        shoppingcart.setOwnName(name);
        shoppingcart.setOrderId(orderId);
        return shoppingCartDao.selectByShopping(shoppingcart);
    }

    @Override
    public boolean update(ShoppingCart shoppingcart) {
        return shoppingCartDao.updateByPrimaryKeySelective(shoppingcart) == 1;
    }

    @Override
    public boolean add(ShoppingCart shoppingcart) {
        return shoppingCartDao.insertSelective(shoppingcart) == 1;
    }

    @Override
    public boolean delete(Integer id) {
        return shoppingCartDao.deleteByPrimaryKey(id) == 1;
    }

    @Override
    public List<ShoppingModel> selectByUsername() {
        UserDetails principal = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        String name = principal.getUsername();
        ShoppingCart shoppingcart = new ShoppingCart();
        shoppingcart.setOwnName(name);
        return shoppingCartDao.selectByShopping(shoppingcart);
    }
}
